package generator.utis;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {

    //注意最后面的/ 要加上 否则地址栏串文件名时会少一个/
    private static final String ALI_DOMAIN="http://test-oss-lzp.oss-cn-guangzhou.aliyuncs.com/";
    public static String uploadImage(MultipartFile file) throws IOException {
        String originalFileName = file.getOriginalFilename();
        String ext = "."+ FilenameUtils.getExtension(originalFileName);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName  = uuid + ext;
        String endpoint ="http://oss-cn-guangzhou.aliyuncs.com/";
        String accessKeyId="LTAI5tAPBSuuiU5QMsDn59t2";
        String accessKeySecret="Ne7zigclvp7dwaH9AHDhVgYMB1bJU7";

        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject("test-oss-lzp",fileName,file.getInputStream());
        ossClient.shutdown();
        return ALI_DOMAIN+fileName;
    }
}